package leetcode.code1590;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public int minSubarray(int[] nums, int p) {
		int mod = 0;
		for (int num : nums) {
			mod = (mod + num) % p;
		}
		if (mod == 0) {
			return 0;
		}
		int ans = Integer.MAX_VALUE;
		Map<Integer, Integer> map = new HashMap<>();
		map.put(0, -1);
		for (int i = 0, sum = 0; i < nums.length; i++) {
			sum = (sum + nums[i]) % p;
			int find = (sum - mod + p) % p;
			if (map.containsKey(find)) {
				ans = Math.min(ans, i - map.get(find));
			}
			map.put(sum, i);
		}
		return ans == Integer.MAX_VALUE || ans == nums.length ? -1 : ans;
	}

	@Override
	public void debug4() {
		H.compare(-1, this.minSubarray(H.str2array3("[1,2,3]"), 7));
//		H.compare(2, this.minSubarray(H.str2array3("[2,16,16,10,10,6,4,1,8,19]"), 36));
//		H.compare(7, this.minSubarray(H.str2array3("[8,32,31,18,34,20,21,13,1,27,23,22,11,15,30,4,2]"), 148));
//		H.compare(0, this.minSubarray(H.str2array3("[1000000000,1000000000,1000000000]"), 3));
//		H.compare(1, this.minSubarray(H.str2array3("[3,1,4,2]"), 6));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
